/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package cart.hibernate.member;

/**
 *
 * @author BibooChung
 */
import cart.hibernate.HibernateUtil;
import cart.hibernate.membershipTier.*;
import cart.hibernate.order.*;
import cart.hibernate.member.*;
import java.util.Date;

import java.util.List; 
import java.util.ArrayList;
import java.util.Iterator; 
import java.util.Set;
import java.util.HashSet;
 
import org.hibernate.HibernateException; 
import org.hibernate.Session; 
import org.hibernate.Transaction;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import com.mysql.jdbc.exceptions.jdbc4.*;

public class ManageMember {
    public static void main(String[] args){
        
        Date date = new Date();
        long point = 234;
        long id=1;
         ManageMember manager = new ManageMember();   
            manager.addMember("aNewNajme yeah", "password", "email", 
        'M', "homePhoneNumber", "cellPhoneNumber", 
        date, "nickName", point, 
        "firstName", "middleName", "lastName", 1);
       /* Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction tx = null;


        try{
            tx = session.beginTransaction();
            MembershipTier membershipTier = (MembershipTier)session.get(MembershipTier.class, (long) 2); 
            
            System.out.println("--------------------------------------------------------------------------------------");
            Date date = new Date();
            
            Member member = new Member("loasdfginasdfasdfName", "password", "email", 
        'M', "homePhoneNumber", "cellPhoneNumber", 
        date, "nickName", 12314, 
        "firstName", "middleName", "lastName", membershipTier);
            
            
       //     session.save(member);
            System.out.println("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
            tx.commit();
        }catch (HibernateException e) {
           if (tx!=null) tx.rollback();
           e.printStackTrace(); 
        } finally {
           session.close(); 
        }*/
    }


    public List getAllMember() {
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction tx = null;
        List allMember = null;
        try{
           tx = session.beginTransaction();
           allMember = session.createQuery("FROM Member").list(); 
           tx.commit();
        }catch (HibernateException e) {
           if (tx!=null) tx.rollback();
           e.printStackTrace(); 
        }finally {
           session.close(); 
        }
        return allMember;
    }
    
    
    public String addMember(String loginName, String password, String email, 
                        char gender, String homePhoneNumber, String cellPhoneNumber, 
                        Date birthDate, String nickName, long point, 
                        String firstName, String middleName, String lastName, long membershipTierId) { 
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction tx = null;

        try {
            tx = session.beginTransaction();
            MembershipTier membershipTier = (MembershipTier)session.get(MembershipTier.class, membershipTierId); 

            Member member = new Member(loginName, password, email, gender, homePhoneNumber, cellPhoneNumber, 
                                    birthDate, nickName, point, firstName, middleName, lastName,membershipTier);
            loginName = (String) session.save(member);
            tx.commit();
        } catch (HibernateException e) {
            if (tx != null) {
                tx.rollback();
            }
            e.printStackTrace();
        } finally {
            session.close();
        }
        return loginName;
    }
    
    public void updateMember(String loginName, String password, String email, 
                        char gender, String homePhoneNumber, String cellPhoneNumber, 
                        Date birthDate, String nickName, long point, 
                        String firstName, String middleName, String lastName, long membershipTierId) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction tx = null;
        try{
            tx = session.beginTransaction();

            Member member = (Member)session.get(Member.class, loginName); 

            MembershipTier membershipTier = (MembershipTier)session.get(MembershipTier.class, membershipTierId); 

            Member newMember = new Member(loginName, password, email, gender, homePhoneNumber, cellPhoneNumber, 
                                    birthDate, nickName, point, firstName, middleName, lastName, membershipTier);

            member.setMember(newMember);

            session.update(member);
            
            tx.commit();
        }catch (HibernateException e) {
           if (tx!=null) tx.rollback();
           e.printStackTrace(); 
        }finally {
           session.close(); 
        }
    }
    
    public void updateMember(String loginName, String password, String email, 
                        char gender, String homePhoneNumber, String cellPhoneNumber, 
                        Date birthDate, String nickName, long point, 
                        String firstName, String middleName, String lastName, long membershipTierId, List orderId) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction tx = null;
        try{
            tx = session.beginTransaction();

            Member member = (Member)session.get(Member.class, loginName); 

            MembershipTier membershipTier = (MembershipTier)session.get(MembershipTier.class, membershipTierId); 

            Member newMember = new Member(loginName, password, email, gender, homePhoneNumber, cellPhoneNumber, 
                                    birthDate, nickName, point, firstName, middleName, lastName, membershipTier);

            Set<Order> orders = new HashSet<Order>();
            ArrayList orderIdList = new ArrayList(orderId);           
            for (int i = 0 ; i < orderIdList.size() ; i++)
            {
              long id = (Long)(orderIdList.get(i));
              Order order = new Order((Order)session.get(Order.class, id));
              orders.add(order);
            }

            member.getOrders().clear();
            member.getOrders().addAll(orders);



            member.setMember(newMember);

            session.update(member);
            
            tx.commit();
        }catch (HibernateException e) {
           if (tx!=null) tx.rollback();
           e.printStackTrace(); 
        }finally {
           session.close(); 
        }
    }
    
    public void updateMember(String loginName, String password, String email, 
                        char gender, String homePhoneNumber, String cellPhoneNumber, 
                        Date birthDate, String nickName, long point, 
                        String firstName, String middleName, String lastName, long membershipTierId,
                        long orderId) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction tx = null;
        try{
            tx = session.beginTransaction();

            Member member = (Member)session.get(Member.class, loginName); 

            MembershipTier membershipTier = (MembershipTier)session.get(MembershipTier.class, membershipTierId); 
            Order order = (Order)session.get(Order.class, orderId); 

            Member newMember = new Member(loginName, password, email, gender, homePhoneNumber, cellPhoneNumber, 
                                    birthDate, nickName, point, firstName, middleName, lastName, membershipTier);
            member.setMember(newMember);
            member.getOrders().add(order);

            session.update(member);
            
            tx.commit();
        }catch (HibernateException e) {
           if (tx!=null) tx.rollback();
           e.printStackTrace(); 
        }finally {
           session.close(); 
        }
    }

    public void updateMember(String loginName, long orderId) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction tx = null;
        try{
            tx = session.beginTransaction();

            Member member = (Member)session.get(Member.class, loginName); 
            Order order = (Order)session.get(Order.class, orderId); 
            member.getOrders().add(order);

            session.update(member);
            
            tx.commit();
        }catch (HibernateException e) {
           if (tx!=null) tx.rollback();
           e.printStackTrace(); 
        }finally {
           session.close(); 
        }
    }

    public boolean deleteMember(String loginName) {
      Session session = HibernateUtil.getSessionFactory().openSession();
      Transaction tx = null;
      try{
         tx = session.beginTransaction();
         Member member = (Member)session.get(Member.class, loginName); 
         if (!member.getOrders().isEmpty())
             return false;
         session.delete(member); 
         tx.commit();
      }catch (HibernateException e) {
         if (tx!=null) tx.rollback();
         e.printStackTrace(); 
      }finally {
         session.close(); 
      }
      return true;
    }



}
